package com.gitee.zzh.codegenerator;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.zzh.common.ConfigUtil;
import com.gitee.zzh.common.StrUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;

public class CodeFromTemplateFile {

    private static JSONObject getConfigJSON() {
        String configFileContent = ConfigUtil.getConfigFileContent();
        return JSONObject.parseObject(configFileContent);
    }

    public static int generateModuleFileCode(ParamInfo paramInfo) {
        int cnt = 0;
        String tableName = paramInfo.getTableName();
        String path = paramInfo.getProjectPath();
        JSONArray jsonArray = getConfigJSON().getJSONArray("packages");
        for (int i = 0; i < jsonArray.size(); i++) {
            Set<String> set = jsonArray.getJSONObject(i).keySet();
            for (String suffixPackage : set) {
                String currentDirs = suffixPackage.replace(".","/");
                try {
                    String currentDir = path + "/" + currentDirs;
                    if (!new File(currentDir).exists()) {
                        Files.createDirectories(Paths.get(currentDir));
                    }
                    JSONArray jsonArray1 = jsonArray.getJSONObject(i).getJSONArray(suffixPackage);
                    for (Object o : jsonArray1) {
                        JSONObject jsonObject = (JSONObject) o;
                        String fileSuffix = jsonObject.getString("fileSuffix");
                        String fileTemplate = jsonObject.getString("fileTemplate");
                        String fileName = paramInfo.getTxtEntityName() + fileSuffix;
                        String content = Files.readString(Paths.get("template/" + fileTemplate));
                        String commonPackages = paramInfo.getCommonPackages();
                        String packages = paramInfo.getPackages();
                        content = content.replace("{commonPackages}", commonPackages);
                        content = content.replace("{packages}", packages);
                        String txtEntityName = paramInfo.getTxtEntityName();
                        String txtEntityFieldName = paramInfo.getTxtEntityFieldName();
                        content = content.replace("{txtEntityName}", txtEntityName);
                        content = content.replace("{txtEntityFieldName}", txtEntityFieldName);
                        String tableRemark = paramInfo.getTableRemarks();
                        content = content.replace("{tableRemark}", tableRemark);
                        content = content.replace("{tableName}", tableName);
                        CustomCode customCode = paramInfo.getCustomCode();
                        String resultMapList = customCode.getResultMapList();
                        if(content.contains("{resultMapList}")) {
                            content = content.replace("{resultMapList}", resultMapList);
                        }
                        String fieldVOList = customCode.getFieldVOList();
                        if(content.contains("{fieldVOList}")) {
                            content = content.replace("{fieldVOList}", fieldVOList);
                        }
                        String fieldEntityList = customCode.getFieldEntityList();
                        if(content.contains("{fieldEntityList}")) {
                            content = content.replace("{fieldEntityList}", fieldEntityList);
                        }
                        String entityToVOSetterFields = customCode.getEntityToVOSetterFields();
                        if(content.contains("{entityToVOSetterFields}")) {
                            content = content.replace("{entityToVOSetterFields}", entityToVOSetterFields);
                        }
                        String voToEntitySetterFields = customCode.getVOToEntitySetterFields();
                        if(content.contains("{VOToEntitySetterFields}")) {
                            content = content.replace("{VOToEntitySetterFields}", voToEntitySetterFields);
                        }
                        String insertValueList = customCode.getInsertValueList();
                        if(content.contains("{insertValueList}")) {
                            content = content.replace("{insertValueList}", insertValueList);
                        }
                        String whereIds = customCode.getWhereIds();
                        if(content.contains("{whereIds}")) {
                            content = content.replace("{whereIds}", whereIds);
                        }
                        String whereIfList = customCode.getWhereIfList("");
                        if(content.contains("{whereIfList}")) {
                            content = content.replace("{whereIfList}", whereIfList);
                        }
                        String columnListWithAlias = customCode.getColumnListWithAlias("");
                        if(content.contains("{columnList}")) {
                            content = content.replace("{columnList}", columnListWithAlias);
                        }
                        String setIfList = customCode.getSetIfList();
                        if(content.contains("{setIfList}")) {
                            content = content.replace("{setIfList}", setIfList);
                        }
                        if(!new File(currentDir + "/" + fileName).exists()) {
                            Files.writeString(Paths.get(currentDir + "/" + fileName), content);
                            cnt++;
                        }
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return cnt;
    }

    public static void generateCommonFileCode(String path) {
        JSONArray jsonArray = getConfigJSON().getJSONArray("commonPackages");
        for (int i = 0; i < jsonArray.size(); i++) {
            Set<String> set = jsonArray.getJSONObject(i).keySet();
            for (String suffixPackage : set) {
                String currentDirs = suffixPackage.replace(".","/");
                try {
                    String currentDir = path + "/" + currentDirs;
                    if (!new File(currentDir).exists()) {
                        Files.createDirectories(Paths.get(currentDir));
                    }
                    JSONArray jsonArray1 = jsonArray.getJSONObject(i).getJSONArray(suffixPackage);
                    for (Object o : jsonArray1) {
                        JSONObject jsonObject = (JSONObject) o;
                        String fileSuffix = jsonObject.getString("fileSuffix");
                        String fileTemplate = jsonObject.getString("fileTemplate");
                        String content = Files.readString(Paths.get("template/" + fileTemplate));
                        String commonPackages = StrUtil.getBasePackage(path);
                        content = content.replace("{commonPackages}", commonPackages);
                        if(!new File(currentDir + "/" + fileSuffix).exists()) {
                            Files.writeString(Paths.get(currentDir + "/" + fileSuffix), content);
                        }
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
